﻿using System;
using System.Data;
using System.IO;
using System.Text;

namespace FoundationClass.Data
{
    public static class DatabaseManager
    {
        //private static string dataTablePath = ConfigurationSettings.AppSettings["DataTablePath"];
        private static string dataTablePath = string.Concat(Environment.CurrentDirectory, "\\Data\\");
        public static void Write(DataTable dataTable)
        {
            if (!Directory.Exists(dataTablePath))
            {
                throw new Exception(dataTablePath + " does not exist.");
            }
            string filePath = dataTablePath + dataTable.TableName + ".txt";

            try
            {
                int i;
                var sw = new StreamWriter(filePath, false);

                for (i = 0; i < dataTable.Columns.Count - 1; i++)
                {
                    sw.Write(dataTable.Columns[i].ColumnName + "|");
                }
                sw.Write(dataTable.Columns[i].ColumnName);
                sw.WriteLine();

                foreach (DataRow row in dataTable.Rows)
                {
                    object[] array = row.ItemArray;

                    for (i = 0; i < array.Length - 1; i++)
                    {
                        sw.Write(array[i] + "|");
                    }
                    sw.Write(array[i].ToString());
                    sw.WriteLine();
                }

                sw.Close();
            }

            catch (Exception ex)
            {
                throw new Exception("Invalid Operation : ", ex);
            }
        }

        public static DataTable Read(string tableName)
        {
            try
            {
                string filePath = dataTablePath + tableName + ".txt";
                if (!File.Exists(filePath))
                {
                    //using (var fs = File.Open(filePath, FileMode.Create))
                    using(var sw = new StreamWriter(File.Open(filePath, FileMode.Create), Encoding.UTF8))
                    {
                        
                    }
                    Console.WriteLine("{0} does not exist.", filePath);
                    //return null;
                }

                DataTable dt = new DataTable(tableName);
                SetDataColumn(dt);
                using (StreamReader sr = File.OpenText(filePath))
                {
                    String input;
                    int line = 0;

                    while ((input = sr.ReadLine()) != null)
                    {
                        string[] data = input.Split(new[] { '|' });

                        if (line > 0)
                        {
                            int col = 0;
                            DataRow row = dt.NewRow();
                            foreach (var s in data)
                            {
                                row[dt.Columns[col].ColumnName] = s;
                                col++;
                            }
                            dt.Rows.Add(row);
                        }
                        line++;
                    }
                }

                return dt;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public static void SetDataColumn(DataTable dataTable)
        {
            switch (dataTable.TableName)
            {
                case "User":
                    dataTable.Columns.Add("id", typeof(string));
                    dataTable.Columns.Add("username", typeof (string));
                    dataTable.Columns.Add("password", typeof(string));
                    dataTable.Columns.Add("firstname", typeof(string));
                    dataTable.Columns.Add("lastname", typeof (string));
                    dataTable.Columns.Add("nickname", typeof(string));
                    dataTable.Columns.Add("gender", typeof(string));
                    dataTable.Columns.Add("worktime", typeof(string));
                    dataTable.Columns.Add("wage", typeof(string));
                    dataTable.Columns.Add("datestart", typeof(string));
                    dataTable.Columns.Add("position", typeof(string));
                    dataTable.Columns.Add("phone", typeof(string));
                    dataTable.Columns.Add("address", typeof(string));
                    break;
                case "Promotion":
                    dataTable.Columns.Add("id", typeof(string));
                    dataTable.Columns.Add("name", typeof(string));
                    dataTable.Columns.Add("startdate", typeof(string));
                    dataTable.Columns.Add("enddate", typeof(string));
                    dataTable.Columns.Add("starttime", typeof(string));
                    dataTable.Columns.Add("endtime", typeof(string));
                    dataTable.Columns.Add("type", typeof(string));
                    dataTable.Columns.Add("pricerate", typeof(string));
                    dataTable.Columns.Add("fixhour", typeof(string));
                    dataTable.Columns.Add("detail", typeof(string));
                    dataTable.Columns.Add("istotal", typeof(string));
                    break;
                case "Computer":
                    dataTable.Columns.Add("id", typeof(string));
                    dataTable.Columns.Add("cname", typeof(string));
                    dataTable.Columns.Add("specification", typeof(string));
                    dataTable.Columns.Add("status", typeof(string));
                    dataTable.Columns.Add("type", typeof(string));
                    break;
                case "ServiceRecord":
                    dataTable.Columns.Add("recordid", typeof(string));
                    dataTable.Columns.Add("recordstart", typeof(string));
                    dataTable.Columns.Add("recordend", typeof(string));
                    dataTable.Columns.Add("recordstatus", typeof(string));
                    dataTable.Columns.Add("recordamount", typeof(string));
                    dataTable.Columns.Add("computerid", typeof(string));
                    dataTable.Columns.Add("promotionid", typeof(string));
                    dataTable.Columns.Add("paymentid", typeof(string));
                    break;
                case "Payment":
                    dataTable.Columns.Add("id", typeof(string));
                    dataTable.Columns.Add("paidtime", typeof(string));
                    dataTable.Columns.Add("totalamount", typeof(string));
                    dataTable.Columns.Add("userid", typeof(string));
                    break;
            }
        }


    }
}
